package cn.yuan.xiaoyu.testmodule.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.yuan.xiaoyu.R;

/**
 * Created by yukuoyuan on 2017/3/28.
 * 这是一个输入验证码控件的自定义应用
 */

public class InputVcodeView extends RelativeLayout implements TextWatcher {

    private View mContentView;
    private int mMeasuredHeight;
    private TextView[] textViews;
    private EditText et_input_vcode_view;
    /**
     * 用来存储字符串的集合处理类
     */
    private StringBuffer stringBuffer = new StringBuffer();

    public InputVcodeView(Context context) {
        super(context);
        initView(context);
    }

    public InputVcodeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    /**
     * 初始化界面
     *
     * @param context 上下文
     */
    private void initView(Context context) {
        mContentView = View.inflate(context, R.layout.view_input_vcode, null);
        //宽高
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //距离父布局的距离
        lp.setMargins(0, 0, 0, 0);
        //设置布局的宽高的属性
        this.setLayoutParams(lp);
        //设置内容距离布局的边界的尺寸
        this.setPadding(0, 0, 0, 0);
        addView(mContentView);
        textViews = new TextView[4];
        textViews[0] = (TextView) findViewById(R.id.tv_input_vcode_view_1);
        textViews[1] = (TextView) findViewById(R.id.tv_input_vcode_view_2);
        textViews[2] = (TextView) findViewById(R.id.tv_input_vcode_view_3);
        textViews[3] = (TextView) findViewById(R.id.tv_input_vcode_view_4);
        et_input_vcode_view = (EditText) findViewById(R.id.et_input_vcode_view);
        //设置角标隐藏
        // et_input_vcode_view.setCursorVisible(false);
        //测量
        measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //获取高度
        mMeasuredHeight = getMeasuredHeight();
        et_input_vcode_view.addTextChangedListener(this);

    }

    /**
     * 这是一个删除所有的输入框的内容并转换为TextView的展示
     */
    public void deleteAllEditText2TextView() {
        if (stringBuffer.length() < 4) {
            stringBuffer.append(et_input_vcode_view.getText().toString());
        }
        et_input_vcode_view.setText("");
        for (int i = 0; i < stringBuffer.length(); i++) {
            //textViews[i].setText(String.valueOf(stringBuffer.toString().charAt(i)));
            //  textViews[i].setBackgroundResource(R.drawable.bg_view_input_vcode_cheeck);
        }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        deleteAllEditText2TextView();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
